package com.yyx.app1.Question_function.Control.QuestionListView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.yyx.app1.Data.BaseDataManager;
import com.yyx.app1.Data.Question.Question_Activity_Data;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.ImageTool;
import com.yyx.app1.ui.ImageShower_Activity;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/9/15.
 */
@EViewGroup(R.layout.itemview_listview_question)
public class Item_ListView_Question extends LinearLayout {
    private Context context;

    @ViewById
    TextView itemview_message_name,itemview_message_petname,itemview_message_count,itemview_message_time,itemview_message_content,button_goto_chat;

    @ViewById
    ImageView itemview_listview_question_divider,itemview_message_portrait;

    @ViewById
    FrameLayout itemview_message_unread;

    @ViewById
    LinearLayout itemview_message_imagelist;

    public Item_ListView_Question(Context context) {
        super(context);
        this.context=context;
    }

    public Item_ListView_Question(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    public Item_ListView_Question(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    public void bindView(Question_Activity_Data data)
    {
        ImageLoader imageLoader = BaseDataManager.getMyImageLoader(context);
        imageLoader.get(data.getPortraitURL(),imageLoader.getImageListener(itemview_message_portrait, testDate.loadImageRes,testDate.defaultPortrait),
                ImageTool.dip2px(context,40),ImageTool.dip2px(context,40));
        Bundle tag = new Bundle();
        tag.putString("type","URL");
        tag.putString("value",data.getPortraitURL());
        ImageShower_Activity.setTagOnClickListener(itemview_message_portrait,context,tag);
        itemview_message_name.setText(data.getUsername());
        itemview_message_petname.setText(data.getPetname());
        itemview_message_time.setText(data.getTime());
        itemview_message_content.setText(data.getConditionsdescribed());
        double imagecount=data.getImagelistURL().length;
        if (imagecount>9)
        {
            imagecount=9.0;
        }
        itemview_message_imagelist.removeAllViews();
        if (imagecount == 1)
        {
            ImageView imageView=new ImageView(context);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            imageLoader.get(data.getImagelistURL()[0], imageLoader.getImageListener(imageView, testDate.loadImageRes,testDate.defaultMedicationImage)
                    , ImageTool.getScreenSize(context).x - ImageTool.dip2px(context, 66), ImageTool.dip2px(context, 250));
            Bundle tag1 = new Bundle();
            tag1.putString("type", "URL");
            tag1.putString("value", data.getImagelistURL()[0]);
            ImageShower_Activity.setTagOnClickListener(imageView, context, tag1);
            itemview_message_imagelist.addView(imageView);
        }
        else
        {
            for (double i=0;i< imagecount/3.0 ;++i)
            {
                int linewidth = ImageTool.getScreenSize(context).x - ImageTool.dip2px(context, 66);
                int padding = ImageTool.dip2px(context,5);
                int imagewidth = (int) (Math.floor(linewidth-2*padding)/3);
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setPadding(0, padding/2, 0, padding/2);
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                linearLayout.setOrientation(HORIZONTAL);
                itemview_message_imagelist.addView(linearLayout);
                for (int j=0; j<3 ; ++j)
                {
                    ImageView imageView=new ImageView(context);
                    imageView.setLayoutParams(new ViewGroup.LayoutParams(imagewidth,imagewidth));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setBackground(new ColorDrawable(Color.RED));
                    Bundle tag1 = new Bundle();
                    tag1.putString("type", "URL");
                    tag1.putString("value", data.getImagelistURL()[(int) i * 3 + j]);
                    ImageShower_Activity.setTagOnClickListener(imageView, context, tag1);
                    imageLoader.get(data.getImagelistURL()[(int) i * 3 + j], imageLoader.getImageListener(imageView, testDate.loadImageRes,testDate.defaultMedicationImage)
                            , imagewidth,imagewidth);
                    linearLayout.addView(imageView);
                    if ((i*3+j+1)==data.getImagelistURL().length)
                    {
                        break;
                    }
                    if (j!=2)
                    {
                        ImageView divider = new ImageView(context);
                        divider.setLayoutParams(new LinearLayout.LayoutParams(padding, imagewidth));
                        linearLayout.addView(divider);
                    }
                }
            }
        }
    }

    public TextView getButton_goto_chat() {
        return button_goto_chat;
    }

    public void setButtonAddOnClickListener(OnClickListener listener)
    {
        button_goto_chat.setOnClickListener(listener);
    }
}
